package deviceinfo.wsm.com.deviceinfo.sdcard;

import android.app.ActivityManager;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import java.io.File;
import java.util.LinkedHashMap;

import deviceinfo.wsm.com.deviceinfo.WsmDeviceInfoSDK;
import deviceinfo.wsm.com.deviceinfo.WsmSDK;
import deviceinfo.wsm.com.deviceinfo.common.FormatUtils;

/**
 * Created by 王家宝 on 2018/7/5.
 * TEL:18241150668
 * MAIL:639586819@qq.com
 */

public class SDCardInfoUtils {

    public static LinkedHashMap<String, String> getAll() {
        LinkedHashMap<String, String> listData = new LinkedHashMap<>();

        listData.put("rom总大小", FormatUtils.formatSize(getRomMemroy()[0]));
        listData.put("rom可用大小", FormatUtils.formatSize(getRomMemroy()[1]));
        listData.put("rom已用大小", FormatUtils.formatSize(getRomMemroy()[0] - getRomMemroy()[1]));
        listData.put("sdcard总大小", FormatUtils.formatSize(getSDCardMemory()[0]));
        listData.put("sdcard可用大小", FormatUtils.formatSize(getSDCardMemory()[1]));
        listData.put("内存总大小", FormatUtils.formatSize(getSysteTotalMemorySize()));
        listData.put("内存已使用", FormatUtils.formatSize(getSysteTotalMemorySize()-getSystemAvaialbeMemorySize()));
        listData.put("最大内存分配", getMaxHeap() + "MB");

        return listData;

    }


    /**
     * 获取系统内存大小
     *
     * @return
     */
    private static long getSysteTotalMemorySize() {
        //获得ActivityManager服务的对象
        ActivityManager mActivityManager = (ActivityManager) WsmDeviceInfoSDK.
                getWsmSDK().getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
        //获得MemoryInfo对象
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        //获得系统可用内存，保存在MemoryInfo对象上
        mActivityManager.getMemoryInfo(memoryInfo);
        //字符类型转换
        return memoryInfo.totalMem;
    }

    private static long getSystemAvaialbeMemorySize() {
        //获得ActivityManager服务的对象
        ActivityManager mActivityManager = (ActivityManager) WsmDeviceInfoSDK.getWsmSDK().
                getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
        //获得MemoryInfo对象
        ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
        //获得系统可用内存，保存在MemoryInfo对象上
        mActivityManager.getMemoryInfo(memoryInfo);
        return memoryInfo.availMem;
    }

    private static long[] getSDCardMemory() {
        long[] sdCardInfo = new long[2];
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            File sdcardDir = Environment.getExternalStorageDirectory();
            StatFs sf = new StatFs(sdcardDir.getPath());
            long bSize = sf.getBlockSize();
            long bCount = sf.getBlockCount();
            long availBlocks = sf.getAvailableBlocks();

            sdCardInfo[0] = bSize * bCount;//总大小
            sdCardInfo[1] = bSize * availBlocks;//可用大小
        }
        return sdCardInfo;
    }

    private static long[] getRomMemroy() {
        long[] romInfo = new long[2];
        //Total rom memory
        romInfo[0] = getTotalInternalMemorySize();

        //Available rom memory
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long availableBlocks = stat.getAvailableBlocks();
        romInfo[1] = blockSize * availableBlocks;
        return romInfo;
    }

    private static long getTotalInternalMemorySize() {
        File path = Environment.getDataDirectory();
        StatFs stat = new StatFs(path.getPath());
        long blockSize = stat.getBlockSize();
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize;
    }


    private static int getMaxHeap() {
        int memClass = 0;//64，以m为单位
        try {
            ActivityManager activityManager = (ActivityManager) WsmDeviceInfoSDK.getWsmSDK().getAppContext().getSystemService(Context.ACTIVITY_SERVICE);
            memClass = activityManager.getMemoryClass();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return memClass;
    }
}
